<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2023/12/19
 * Time: 15:05
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Address extends Base
{
    /**
     * 工厂收货地址列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $where['addr_status'] = 1;
            $where['addr_type'] = 2;

            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('address')
                ->join('up_area a1', 'a1.district_id = addr_sheng', 'LEFT')
                ->join('up_area a2', 'a2.district_id = addr_shi', 'LEFT')
                ->join('up_area a3', 'a3.district_id = addr_qu', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('address')
                ->join('up_area a1', 'a1.district_id = addr_sheng', 'LEFT')
                ->join('up_area a2', 'a2.district_id = addr_shi', 'LEFT')
                ->join('up_area a3', 'a3.district_id = addr_qu', 'LEFT')
                ->where($where)
                ->field('addr_id, addr_username, addr_phone, addr_time, 
                concat(a1.district, a2.district, a3.district, addr_address) address')
                ->page($NowPage, $limits)
                ->order('addr_time desc')
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 新增厂家收货地址
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addressAdd(){
        if(Request::instance()->isPost()){
            $addr_username = input('addr_username');
            $addr_phone = input('addr_phone');
            $addr_sheng = input('addr_sheng');
            $addr_shi = input('addr_shi');
            $addr_qu = input('addr_qu');
            $addr_address = input('addr_address');

            $insert = [
                'addr_username' => $addr_username,
                'addr_phone' => $addr_phone,
                'addr_type' => 2,
                'addr_sheng' => $addr_sheng,
                'addr_shi' => $addr_shi,
                'addr_qu' => $addr_qu,
                'addr_address' => $addr_address,
                'addr_time' => createTime()
            ];
            $res = Db::table('up_address')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        return $this->fetch('', ['province' => $province]);
    }

    /**
     * 编辑收货地址
     * @return mixed|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function addressUpdate(){
        $addr_id = input('addr_id');
        if(Request::instance()->isPost()){
            $addr_username = input('addr_username');
            $addr_phone = input('addr_phone');
            $addr_sheng = input('addr_sheng');
            $addr_shi = input('addr_shi');
            $addr_qu = input('addr_qu');
            $addr_address = input('addr_address');

            $update = [
                'addr_id' => $addr_id,
                'addr_username' => $addr_username,
                'addr_phone' => $addr_phone,
                'addr_sheng' => $addr_sheng,
                'addr_shi' => $addr_shi,
                'addr_qu' => $addr_qu,
                'addr_address' => $addr_address,
                'addr_update_time' => createTime()
            ];
            $res = Db::table('up_address')->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $address = Db::table('up_address')
            ->where('addr_id', $addr_id)
            ->field('addr_username, addr_phone, addr_sheng, addr_shi, addr_qu, addr_address')
            ->find();
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        $city = Db::table('up_area')->where('pid', $address['addr_sheng'])->field('district_id, district')->select();
        $qu = Db::table('up_area')->where('pid', $address['addr_shi'])->field('district_id, district')->select();
        $this->assign([
            'address' => $address,
            'province' => $province,
            'city' => $city,
            'qu' => $qu,
            'addr_id' => $addr_id
        ]);
        return $this->fetch();
    }

    /**
     * 删除收货地址
     * @return \think\response\Json
     */
    public function addressDel(){
        $addr_id = input('id');
        $res = Db::table('up_address')->where('addr_id', $addr_id)->setField('addr_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}